﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Sekolah
{
    public partial class MenuAbsenForm : Form
    {
        private string _nama;
        private string _databaseConnection;
        private string _nomorInduk;
        public MenuAbsenForm(string nama, string NIP, string masaKerja, string defaultButton)
        {
            InitializeComponent();
            foreach (Control item in panel1.Controls)
            {
                Button btn = item as Button;
                if (btn != null && btn.Text.ToUpper() == defaultButton.ToUpper())
                {
                    btn.TabIndex = 0;
                    AcceptButton = btn;
                    break;
                }
            }

            showHideBulanTahun();
            _nama = nama.Replace("'", "''");
            _nomorInduk = NIP;
            label1.Text = "Login Sebagai : " + nama;
            label2.Text = "Masa Kerja : " + masaKerja;
            _databaseConnection = AppConfig.ReadSetting(AppConfig.DATABASE_KEY);
            for (int i = 1; i <= 12; i++)
            {
                cbBulan.Items.Add(i);
            }
            cbBulan.SelectedItem = DateTime.Now.Month;

            for (int i = DateTime.Now.Year - 3; i <= DateTime.Now.Year; i++)
            {
                cbTahun.Items.Add(i);
            }
            cbTahun.SelectedItem = DateTime.Now.Year;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                string keterangan = btn.Text;
                using (var cn = new SqlConnection(_databaseConnection))
                {
                    cn.Open();
                    var cmd = new SqlCommand(
                            "INSERT INTO CARD_ABSEN (NOMOR_INDUK, NAMA, TANGGAL, KETERANGAN) SELECT '" + _nomorInduk +
                            "', '" + _nama + "', GETDATE(), '" + keterangan + "'", cn);
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
            }
            Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            showHideBulanTahun();
            var sql = "sp_card_data_guru";
            var dt = new DataTable();
            using (var da = new SqlDataAdapter(sql, _databaseConnection))
            {
                da.Fill(dt);
            }
            dataGridView1.DataSource = dt;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            showHideBulanTahun();
            var sql = "sp_card_data_siswa";
            var dt = new DataTable();
            using (var da = new SqlDataAdapter(sql, _databaseConnection))
            {
                da.Fill(dt);
            }
            dataGridView1.DataSource = dt;
        }

        private void showHideBulanTahun(bool show = false)
        {
            label3.Visible = show;
            label4.Visible = show;
            cbBulan.Visible = show;
            cbTahun.Visible = show;
            button11.Visible = show;
            dataGridView1.Top = show ? 37 : 6;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            showHideBulanTahun(true);
            dataGridView1.DataSource = null;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            showHideBulanTahun();
                //sp_card_data_kelas '197910212012071602015'
            var sql = "sp_card_data_kelas '" + _nomorInduk + "'";
            var dt = new DataTable();
            using (var da = new SqlDataAdapter(sql, _databaseConnection))
            {
                da.Fill(dt);
            }
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button11_Click(object sender, EventArgs e)
        {
            //sp_record_kehadiran 2, 2015, '198902142012071602016'
            var sql = "sp_record_kehadiran "+cbBulan.SelectedItem.ToString()+", " + cbTahun.SelectedItem.ToString() + ",'" + _nomorInduk + "'";
            var dt = new DataTable();
            using (var da = new SqlDataAdapter(sql, _databaseConnection))
            {
                da.Fill(dt);
            }
            dataGridView1.DataSource = new DataTable();
            dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete;
            dataGridView1.DataSource = dt;
            dataGridView1.Columns["TERLAMBAT"].Visible = false;
        }

        private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView1.DataBindingComplete -= DataGridView1_DataBindingComplete;
            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                DataRowView drv = dgvr.DataBoundItem as DataRowView;
                if (drv != null)
                {
                    DataRow dr = drv.Row;
                    if (dr != null)
                    {
                        if (dr["TERLAMBAT"].Equals(1))
                        {
                            dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
                        }
                    }
                }
            }
        }

        private void btnTutup_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
